/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package person.mail;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.io.Serializable;
import java.sql.SQLException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import system.general.Exc;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author gilberto
 */
@ManagedBean(name = "person_mail")
@RequestScoped
public class Main extends Beans implements Serializable {

    private Properties props;
    private String from;
    private String to;
    private String subject;
    private String message;
    private system.general.Message msg = new system.general.Message();
    private Map<String, String> map = new LinkedHashMap<>();
    private system.mail_configuration.Find config_mailFind = new system.mail_configuration.Find();
    private List<system.mail_configuration.Beans> config_mailList = new ArrayList<>();

    @PostConstruct
    public void configuration() {
        try {
            map.put("id  = 1", " ");

            config_mailList.clear();
            config_mailList = config_mailFind.byMap(map);
            this.getConfigBeans().setClave(config_mailList.get(0).getClave());

            this.getConfigBeans().setCorreo(config_mailList.get(0).getCorreo());
            this.getConfigBeans().setPuerto(config_mailList.get(0).getPuerto());

            props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.socketFactory.port", this.getConfigBeans().getPuerto());
            props.put("mail.smtp.socketFactory.class",
                    "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", this.getConfigBeans().getPuerto());
        } catch (Exc ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void sendMail() {
        try {

            Session session = Session.getInstance(props, new javax.mail.Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(getConfigBeans().getCorreo(), getConfigBeans().getClave());
                }
            });



            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(getConfigBeans().getCorreo()));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(getDestinatario()));
            message.setSubject(" ");
            message.setText(getMensaje());

            Transport.send(message);

            throw new Exc("m1");


        } catch (Exc ex) {
            msg.showMsg(namePackage(), "system", ex.getIdError());
        } catch (Exception ex) {
            msg.showErr(namePackage(), "java", ex);
        } finally {
        }
    }

    private String namePackage() {
        Package pack = this.getClass().getPackage();
        return pack.getName();
    }
    // Getters y Setters de los atributos
    // de la clase: to, from, subject, message
    // ...
}